﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using DoAnWeb.DTO;

namespace DoAnWeb.DAO
{
    public class CT_SanPhamDAO
    {
        DataProvider dp = new DataProvider();
        public BindingList<CT_SanPhamDTO> GetList()
        {
            if (dp.OpenConnect())
            {
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_CT_SanPham", null);
                dp.CloseConnect();
                BindingList<CT_SanPhamDTO> List = new BindingList<CT_SanPhamDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    CT_SanPhamDTO DTO = new CT_SanPhamDTO();
                    DTO.MaSanPham = int.Parse(row["MaSanPham"].ToString());
                    DTO.NoiDung = row["NoiDung"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }
        public CT_SanPhamDTO GetFromId(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaCTDonHang", _Ma) };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetFromID_CT_SanPham", null);
                dp.CloseConnect();
                CT_SanPhamDTO DTO = new CT_SanPhamDTO();
                foreach (DataRow row in dt.Rows)
                {
                    DTO.MaSanPham = int.Parse(row["MaSanPham"].ToString());
                    DTO.NoiDung = row["NoiDung"].ToString();
                    DTO.HinhAnh = row["HinhAnh"].ToString();
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                    {
                        DTO.DaXoa = "Bình thường";
                    }
                    else
                    {
                        DTO.DaXoa = "Đã Xóa";
                    }
                }
                return DTO;
            }
            return null;
        }
        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaCTSanPham", _Ma) };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_CT_SanPham", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
        public bool UnDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaCTSanPham", _Ma) };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnDelete_CT_SanPham", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
        public bool Add(CT_SanPhamDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@NoiDung", _DTO.NoiDung), new SqlParameter("@HinhAnh", _DTO.HinhAnh), new SqlParameter("@GiaGoc", _DTO.GiaGoc) ,
                                                new SqlParameter("@DiaChi", _DTO.DiaChi),new SqlParameter("@DienThoai", _DTO.DienThoai),new SqlParameter("@DienThoai", _DTO.DienThoai),new SqlParameter("@DaXoa", _DTO.DaXoa)
                                              };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_CT_SanPham", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }
        public bool Update(CT_SanPhamDTO _DTO)
        {
            bool kq;
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaSanPham", _DTO.MaSanPham),new SqlParameter("@NoiDung", _DTO.NoiDung), new SqlParameter("@HinhAnh", _DTO.HinhAnh), new SqlParameter("@GiaGoc", _DTO.GiaGoc) ,
                                                new SqlParameter("@DiaChi", _DTO.DiaChi),new SqlParameter("@DienThoai", _DTO.DienThoai),new SqlParameter("@DienThoai", _DTO.DienThoai),new SqlParameter("@DaXoa", _DTO.DaXoa)
                                              };
                kq = dp.SqlExcuteNonQuery("uproc_Update_CT_SanPham", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
    }
}